Reverse Words in a Given String (gfg)
Problem Descriptionβ
Given a string S
, you need to reverse the order of words in the string. Words are separated by dots.
Examplesβ
Example 1:
Input: S = "i.like.this.program.very.much"
Output: "much.very.program.this.like.i"
Explanation: After reversing the words, the new string is "much.very.program.this.like.i".
Example 2:
Input: S = "pqr.mno"
Output: "mno.pqr"
Explanation: After reversing the words, the new string is "mno.pqr".
Your Taskβ
You don't need to read input or print anything. Your task is to complete the function reverseWords()
which takes the string S
as input and returns the string with the words reversed.
Expected Time Complexity:
Expected Auxiliary Space:
Constraintsβ
1 β€ |S| β€ 2000
Problem Explanationβ
The problem is to reverse the order of words in a string where words are separated by dots.
Code Implementationβ
- Python
- C++
class Solution:
def reverseWords(self, S):
# Split the string by dots
words = S.split('.')
# Reverse the list of words
words.reverse()
# Join the reversed list back into a string with dots
return '.'.join(words)
# Example usage
if __name__ == "__main__":
solution = Solution()
print(solution.reverseWords("i.like.this.program.very.much")) # Expected output: "much.very.program.this.like.i"
print(solution.reverseWords("pqr.mno")) # Expected output: "mno.pqr"
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
// Function to reverse words in a given string.
string reverseWords(string S) {
// Split the string by dots
vector<string> words;
string word;
for (char c : S) {
if (c == '.') {
words.push_back(word);
word.clear();
} else {
word += c;
}
}
words.push_back(word); // Add the last word
// Reverse the list of words
reverse(words.begin(), words.end());
// Join the reversed list back into a string with dots
string result;
for (size_t i = 0; i < words.size(); ++i) {
if (i != 0) {
result += '.';
}
result += words[i];
}
return result;
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
string s;
cin >> s;
Solution obj;
cout << obj.reverseWords(s) << endl;
}
return 0;
}
// } Driver Code Ends
Example Walkthroughβ
For the string S = "i.like.this.program.very.much"
:
- Split the string by dots into
["i", "like", "this", "program", "very", "much"]
. - Reverse the list to
["much", "very", "program", "this", "like", "i"]
. - Join the reversed list with dots to get
"much.very.program.this.like.i"
.
For the string S = "pqr.mno"
:
- Split the string by dots into
["pqr", "mno"]
. - Reverse the list to
["mno", "pqr"]
. - Join the reversed list with dots to get
"mno.pqr"
.
Solution Logic:β
- Split the string into words using the dot as a delimiter.
- Reverse the list of words.
- Join the reversed list back into a string with dots.
Time Complexityβ
- The time complexity is , where |S| is the length of the input string.
Space Complexityβ
- The auxiliary space complexity is due to the storage required for the list of words.
Referencesβ
- gfg Problem: gfg Problem
- Solution Author: arunimad6yuq/